package org.tang.springcloudvv.config;

import ch.qos.logback.classic.Logger;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.tang.springcloudvv.cache.CacheMqQueues;
import org.tang.springcloudvv.mapperextend.ExtLog4jMapper;
import org.tang.springcloudvv.model.Log4jSlowSql;
import org.tang.springcloudvv.model.Log4jj;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 定时器消费日志队列
 */
@Configuration
public class CommonTimerTask {

protected final Logger logger = (Logger) LoggerFactory.getLogger(getClass());

    @Autowired
    private ExtLog4jMapper extLog4jMapper;

	@Value("${SLOW_SQL_TIME:2000}")
	private Long SLOW_SQL_TIME;
	public static Long SLOW_SQL_TIME_ = 2000L;
	public static int last_insert_size = 0;

	private static long showLogLastTime = 0;

	private final static ExecutorService executor = Executors.newSingleThreadExecutor(new BasicThreadFactory.Builder().namingPattern("CommonTimerTask%d").build());

	@PostConstruct
	public void subscribe() {

		SLOW_SQL_TIME_ = SLOW_SQL_TIME;

		executor.execute(new Runnable() {

			@Override
			public void run() {

				while (true) 
				{
					log();

					if (last_insert_size < 1)
					{
						sleep(1000*10);
					}
					try 
					{
						dealLog4jSlowSql();
						dealLog4j();
					} catch (Throwable e) {
						logger.error("",e);
					}
				}
			}
		});
	}

	private void log()
	{
		if (System.currentTimeMillis() - showLogLastTime > 1000*60)
		{
			showLogLastTime = System.currentTimeMillis();
			logger.info("CommonTimerTaskLog4j===========SLOW_SQL_TIME:"+SLOW_SQL_TIME_);
		}
	}
	
	private void dealLog4jSlowSql()
	{
		List<Log4jSlowSql> list = new ArrayList<>();
		//最多一次消费500条
		for (int i = 0; i < 500; i++) {
			Log4jSlowSql log4jSlowSql =  (Log4jSlowSql)CacheMqQueues.poll(CacheMqQueues.log4jSlowSql);
			if (log4jSlowSql != null)
			{
				list.add(log4jSlowSql);
			}
			else
			{
				break;
			}
		}
		if (list.size()>0)
		{
			last_insert_size = extLog4jMapper.addLog4jSlowSql(list);
			logger.info("extLog4jMapper.addLog4jSlowSql="+last_insert_size);
		}
		else
		{
			last_insert_size = 0;
		}

	}

	private void dealLog4j()
	{
		List<Log4jj> list = new ArrayList<>();
		//最多一次消费500条
		for (int i = 0; i < 500; i++) {
			Log4jj log4jj =  (Log4jj)CacheMqQueues.poll(CacheMqQueues.log4j);
			if (log4jj != null)
			{
				list.add(log4jj);
			}
			else
			{
				break;
			}
		}
		if (list.size()>0)
		{
			last_insert_size = extLog4jMapper.adds(list);
			logger.info("extLog4jMapper.adds="+last_insert_size);
		}
		else
		{
			last_insert_size = 0;
		}

	}

	
	private void sleep(long ms) {
		try {
			Thread.sleep(ms);
		} catch (Exception e) {
		}
	}
}
